import ObjectUtil from '../object';

/**
 * 是否包含
 * @param {[]} array
 * @param obj
 * @returns {boolean}
 */
function contains(array: any[], obj: any): boolean {
  for (let i = 0, { length } = array; i < length; i += 1) {
    if (array[i] === obj) {
      return true;
    }
  }
  return false;
}

/**
 * 索引
 * @param {[]} array
 * @param obj
 * @returns {number}
 */
function indexOf(array: any[], obj: any): number {
  for (let i = 0, { length } = array; i < length; i += 1) {
    if (array[i] === obj) {
      return i;
    }
  }
  return -1;
}

/**
 * 删除元素
 * @param {[]} array
 * @param obj
 */
function deleteObject(array: any[], obj: any) {
  const index = indexOf(array, obj);
  if (index > -1) {
    array.splice(index, 1);
  }
}

/**
 * 删除元素
 * @param {[]} array
 * @param index
 */
function deleteIndex(array: any[], index: number) {
  if (index > -1) {
    array.splice(index, 1);
  }
}

/**
 * 比较两个数组内元素是否相等
 * @param arrayOne
 * @param arrayTwo
 * @returns {boolean}
 */
function compare(arrayOne: any, arrayTwo: any): boolean {
  return ObjectUtil.compare(arrayOne, arrayTwo);
}

/**
 * 比较两个数组内元素是否相等
 * @param arrayOne
 * @param arrayTwo
 * @returns {boolean}
 */
function equals(arrayOne: any, arrayTwo: any): boolean {
  return compare(arrayOne, arrayTwo);
}

export { contains, indexOf, deleteObject, deleteIndex, compare, equals };
export default { contains, indexOf, deleteObject, deleteIndex, compare, equals };
